from flask import Blueprint
from app.common.result import trueReturn, falseReturn
from app.api import handle_error, verify_params
from flask import request, g, jsonify
from app.models.Room import Room

room_blueprint = Blueprint('room_blueprint', __name__, url_prefix='/room')


@room_blueprint.before_request
@handle_error
def before_request():
    if (request.method == 'POST' or request.method == 'PUT') and request.get_data():
        g.data = request.get_json()
    elif request.method == 'GET' or request.method == 'DELETE':
        g.data = request.args
    else:
        g.data = {}


@room_blueprint.route('', methods=['GET'])
@handle_error
@verify_params(params=['by'])
def get_rooms():
    by = g.data.get('by', None)
    if by == 'roomId':
        roomId = g.data.get('roomId', None)
        if not roomId:
            return falseReturn(msg='未提供roomId, 请检查')
        rooms = Room.get_by_room_id(roomId=roomId)
        if not rooms:
            return falseReturn(None, '该房间不存在')
        rooms = [rooms]
    elif by == 'all':
        rooms = Room.get_all()
    else:
        return falseReturn(msg='请求方法有误，请检查')

    return trueReturn({
        'rooms': rooms,
        'count': len(rooms)
    })


@room_blueprint.route('', methods=['POST'])
@handle_error
@verify_params(params=['roomId'])
def create_room():
    roomId = g.data.get('roomId')
    number = g.data.get('number', None)
    leaderId = g.data.get('leaderId', None)
    temperature = g.data.get('temperature', None)
    humility = g.data.get('humility', None)
    status = g.data.get('status', None)
    memo = g.data.get('memo', None)
    isDoorOpened = g.data.get('isDoorOpened', None)
    isWaterBoiled = g.data.get('isWaterBoiled', None)
    path = g.data.get('path', None)

    room = Room.get_by_room_id(roomId=roomId)
    if room:
        return falseReturn(msg='房间已存在！')
    else:
        room = Room(roomId=roomId, number=number, leaderId=leaderId, temperature=temperature, humility=humility, status=status, memo=memo, isDoorOpened=isDoorOpened, isWaterBoiled=isWaterBoiled, path=path).save()
        return trueReturn(data={"room": room.json()}, msg='创建成功')


@room_blueprint.route('', methods=['PUT'])
@handle_error
@verify_params(params=['roomId'])
def modify_room():
    print(g.data)
    roomId = g.data.get('roomId')
    number = g.data.get('number', None)
    leaderId = g.data.get('leaderId', None)
    # temperature = g.data.get('temperature', None)
    # humility = g.data.get('humility', None)
    # status = g.data.get('status', None)
    memo = g.data.get('memo', None)
    # isDoorOpened = g.data.get('isDoorOpened', None)
    # isWaterBoiled = g.data.get('isWaterBoiled', None)
    # path = g.data.get('path', None)

    room = Room.get_by_room_id(roomId)
    if not room:
        return falseReturn(msg='房间不存在')
    else:
        room.update(number=number, leaderId=leaderId, memo=memo)
        room.reload()
        return trueReturn(data={"room": room.json()}, msg='修改成功')


@room_blueprint.route('', methods=['DELETE'])
@handle_error
@verify_params(params=['roomId'])
def delete_room():
    roomId = g.data['roomId']

    room = Room.get_by_room_id(roomId=roomId)
    if not room:
        return falseReturn(msg='房间不存在！')
    else:
        room.delete()
        return trueReturn(msg='删除成功！')
